package cn.gwm.flink.streaming.strategy.multidimension;

import cn.gwm.flink.streaming.constant.ChargePredictFields;
import cn.gwm.flink.streaming.strategy.vehiclepredict.DwsStrConstant;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author : GW00280745
 * @date : 2023/4/17 9:56
 * @description : 慢充默认值处理
 */
public class DefaultObc implements Serializable {
    private PhoenixInfo queryUtil = new PhoenixInfo();
    public String process(JSONObject line){
        // 按本车充电记录，结合电流、地点进行测算每1%的充电时间，再进行计算；
        // 按CC码，区分电池容量进行区分，云端根据CC码计算1%的容量均时（后期大容量电池用长时间，小容量电池为小时长）；
        String vin = line.getString(ChargePredictFields.ins().srcFieldVin);
        Float bmsCurr = line.getFloat(ChargePredictFields.ins().srcFieldBmsBatterCurrRm);
        String poiId = line.getString(ChargePredictFields.ins().srcFieldPoiId);
        String aoiId = line.getString(ChargePredictFields.ins().srcFieldAoiId);
        String modelCode = line.getString(ChargePredictFields.ins().srcFieldModelCode);
        //先按本车进行匹配
        String s = oneCarV1(line);
        if (StringUtils.isNotBlank(s)){
            line.put("flag","default-1soc-vehicle");
            return s;
        }
        //再按model进行匹配
        String modelS = oneModelV1(line);
        if (StringUtils.isNotBlank(modelS)){
            line.put("flag","default-1soc-modelcode");
            return modelS;
        }
        return null;
    }
    private String oneCar(JSONObject line){
        //只要地点电流维度
        Float bmsSoc = line.getFloat(ChargePredictFields.ins().srcFieldBmsSoc);
        String startConditionCurr = queryUtil.startQuery1Soc(line);
        List<JSONObject> list = queryUtil.query1Soc(startConditionCurr, line);
        if (list==null || list.size()==0){
            return null;
        }
        JSONObject json = list.get(0);
        Float soc1Avg = json.getFloat(DwsStrConstant.ins().soc1Avgtime);
        float v = soc1Avg * (100 - bmsSoc) ;
        int i = new BigDecimal(v).divide(new BigDecimal(1), 0, BigDecimal.ROUND_HALF_UP).intValue();
        String concat = String.valueOf(bmsSoc).concat(ChargePredictFields.ins().char3).concat("100");
        String s = String.valueOf(i).concat(ChargePredictFields.ins().char3).concat(concat);
        return s;
    }
    private String oneCarV1(JSONObject line){
        Float bmsSoc = line.getFloat(ChargePredictFields.ins().srcFieldBmsSoc);
        String startConditionCurr = queryUtil.startQuery1SocV1(line);
        List<JSONObject> list = queryUtil.query1SocV1(startConditionCurr, line);
        if (list==null || list.size()==0){
            return null;
        }
        JSONObject json = list.get(0);
        Float soc1Avg = json.getFloat(DwsStrConstant.ins().soc1AvgtimeE);
        float v = soc1Avg * (100 - bmsSoc) ;
        int i = new BigDecimal(v).divide(new BigDecimal(1), 0, BigDecimal.ROUND_HALF_UP).intValue();
        String concat = String.valueOf(bmsSoc).concat(ChargePredictFields.ins().char3).concat("100");
        String s = String.valueOf(i).concat(ChargePredictFields.ins().char3).concat(concat);
        return s;
    }
    private String oneModel(JSONObject line){
        Float bmsSoc = line.getFloat(ChargePredictFields.ins().srcFieldBmsSoc);
        String startConditionCurr = queryUtil.startQuery1SocModel(line);
        List<JSONObject> list = queryUtil.query1Soc(startConditionCurr, line);
        if (list==null || list.size()==0){
            return null;
        }
        JSONObject json = list.get(0);
        Float soc1Avg = json.getFloat(DwsStrConstant.ins().modelSoc1Avgtime);
        float v = soc1Avg * (100 - bmsSoc) ;
        int i = new BigDecimal(v).divide(new BigDecimal(1), 0, BigDecimal.ROUND_HALF_UP).intValue();
        String concat = String.valueOf(bmsSoc).concat(ChargePredictFields.ins().char3).concat("100");
        String s = String.valueOf(i).concat(ChargePredictFields.ins().char3).concat(concat);
        return s;
    }

    private String oneModelV1(JSONObject line){
        Float bmsSoc = line.getFloat(ChargePredictFields.ins().srcFieldBmsSoc);
        //同cc码 同电流（用soc和温度计算） 前200条取平均值
        String startConditionCurr = queryUtil.startQuery1SocV2(line);
        List<JSONObject> list = queryUtil.query1SocV2(startConditionCurr, line);
        if (list==null || list.size()==0){
            return null;
        }
        //取list的平均值
        float cnt=0;
        for (JSONObject item : list) {
            Float e = item.getFloat("e");
            cnt +=e;
        }
        Float soc1Avg = 0f;
        soc1Avg = cnt / list.size();
        //
        if (false){
            JSONObject json = list.get(0);
            soc1Avg = json.getFloat(DwsStrConstant.ins().soc1AvgtimeE);
        }
        float v = soc1Avg * (100 - bmsSoc) * 1.05f;
        int i = new BigDecimal(v).divide(new BigDecimal(1), 0, BigDecimal.ROUND_HALF_UP).intValue();
        String concat = String.valueOf(bmsSoc).concat(ChargePredictFields.ins().char3).concat("100");
        String s = String.valueOf(i).concat(ChargePredictFields.ins().char3).concat(concat);
        return s;
    }
}
